import React, { Component } from 'react'
import PubSub from 'pubsub-js'
import Son from './Son'

///接收数据的组件,除了使用Consumer.还可以使用另一种写法
export default class Far extends Component {
  componentDidMount() {
    this.token = PubSub.subscribe('xxx', (topic, data) => {
      console.log('far接收到了', data)
    })
  }

  componentWillUnmount() {
    PubSub.unsubscribe(this.token)
  }
  render() {
    return (
      <div>
        Far
        <button
          onClick={() => {
            PubSub.publish('yyy', '哈哈哈哈')
          }}
        >
          发送yyy话题
        </button>
        <hr />
        <Son></Son>
      </div>
    )
  }
}
